package us.gaaoc.framework.model.util;

import java.io.Serializable;
import java.util.List;

/**
 * @author Sukahda Tapshalkar Jul 26, 2007
 * 
 */
public abstract class SortableList <T> implements Serializable{
	
	/**
	 * column name
	 */
	private String _sort;

	/**
	 * order of the listing
	 */
	private boolean _ascending;
	
	/**
	 * list to sort 
	 */
	private List<T> _data;

	protected SortableList(String defaultSortColumn, List<T> filingList) {
		_sort = defaultSortColumn;
		_ascending = isDefaultAscending(defaultSortColumn);
		_data = filingList;
	}

	/**
	 * Sort the list.
	 */
	protected abstract void sort(String column, boolean ascending, List<T> data);

	/**
	 * Is the default sort direction for the given column "ascending" ?
	 */
	protected abstract boolean isDefaultAscending(String sortColumn);

	/**
	 * This method helps to change the order of the list.
	 * @param sortColumn
	 * @param filingList
	 */
	public void sort(String sortColumn, List<T> filingList) {
		if (sortColumn == null) {
			throw new IllegalArgumentException(
					"Argument sortColumn must not be null.");
		}

		if (_sort.equals(sortColumn)) {
			// current sort equals new sortColumn -> reverse sort order
			_ascending = !_ascending;
		} else {
			// sort new column in default direction
			_sort = sortColumn;
			_ascending = isDefaultAscending(_sort);
		}
		sort(_sort, _ascending, _data);
	}

	public String getSort() {
		return _sort;
	}

	public void setSort(String sort) {
		_sort = sort;
	}

	public boolean isAscending() {
		return _ascending;
	}

	public void setAscending(boolean ascending) {
		_ascending = ascending;
	}
	
	public List<T> getData(){
		return _data;
	}
	
	public void setData(List<T> data) {
		_data =data;
	}
	
}
